/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package quanlysieuthi;
import DAO.*;
import DAO.SubConfig;
import DAO.UserDAO;
import DTO.*;
import Model.CModel;
import java.awt.BorderLayout;
import java.awt.Container;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.swing.JRViewer;

public class QuanLySieuThi {
    public static String user_name ="minhkhang";
    public static String ho_ten = "Nguyen Minh Khang";
    public static long nhanvien_id = 1;
    public static String pathFolderReport ="src/report/";
    public static String formatDate = "dd-MM-yyyy";
    public static long user_id = 0;
    public static int OrderType = 1;
    public static CKho kho = null;
    public static int nam = 0;
    public static int thang = 0;
    public static User _user = null;
    public static HashMap cfParams = null;
    private static Object JRXmloader;
    /**
     * @param args the command line arguments
     */
    @SuppressWarnings("empty-statement")
    public static void main(String[] args) {
        Date d = new Date();        
        QuanLySieuThi.nam = d.getYear() + 1900;
        QuanLySieuThi.thang = d.getMonth() + 1;     
        updateKhoaSo();
        CModel _model = new CModel("");
        cfParams = _model.setConfig();        
        _model.Close();
        if(QuanLySieuThi.cfParams.containsKey("dinhdangngaythang")){
            CConfig cf = (CConfig)QuanLySieuThi.cfParams.get("dinhdangngaythang");
            if(cf!= null){
                String _format = cf.getValue().trim();
                QuanLySieuThi.formatDate = _format;// (String)
            }
        }
        FrmLogin login = new FrmLogin();
        login.setVisible(true);       
    }
    
    private static void updateKhoaSo(){
        CKhoaSoDAO ksDAO = new CKhoaSoDAO();
        SubConfig config = SubConfig.getInstance();
        config.init();
        List<CKhoaSo> khoaso = ksDAO.getAll(config);
        if(khoaso.size() > 0){
            CKhoaSo ks = khoaso.get(0);
            if(QuanLySieuThi.thang == ks.getThang() && QuanLySieuThi.nam == ks.getNam()){
                
            }
            else{
                int thang_truoc = 0;
                int nam_truoc = 0;
                if(QuanLySieuThi.thang == 1){
                    thang_truoc = 12;
                    nam_truoc = QuanLySieuThi.nam - 1;
                }
                else{
                    thang_truoc = QuanLySieuThi.thang - 1;
                    nam_truoc = QuanLySieuThi.nam;
                }
               int t = ksDAO.Khoa_So(thang_truoc, nam_truoc, QuanLySieuThi.thang, QuanLySieuThi.nam, config);
               if(t!= -1){
                    ks.setKhoa_so(true);
                    ksDAO.updateOnSubmint(ks, config);
                    ks.setThang(QuanLySieuThi.thang);
                    ks.setNam(QuanLySieuThi.nam);
                    ksDAO.insertOnSubmit(ks, config);
                    CModel _model = new CModel(QuanLySieuThi.user_name);
                    _model.refresh();
                    _model.Close();
               }
            }
        }
        config.close();
    }
     /**
     * @param path.Truyền vào 1 đường dẫn.
     * @return Trả về 1 đường dẫn lùi lại 2 cấp so với khi truyền vào
     */
    public  static String getPath(String path){ 
        
    	path = path.replaceAll(Matcher.quoteReplacement("\\"), Matcher.quoteReplacement(File.separator));
        System.out.println("Replace \\:" + path);
        path = path.replaceAll(Matcher.quoteReplacement("/"), Matcher.quoteReplacement(File.separator));
        System.out.println("Replace /:" + path);        
           if(path.contains("!"))
        {
        int indexOfExclaim = path.indexOf("!") ;
        String sExclamation = path.substring(indexOfExclaim,path.length());
        System.out.println(sExclamation);
        path = path.replaceAll(Matcher.quoteReplacement(sExclamation), Matcher.quoteReplacement(""));
            int lastindex = path.lastIndexOf(File.separator);
        if (lastindex != -1) {
            path = path.substring(0, lastindex);
        }
        }
        int lastindex = path.lastIndexOf(File.separator);
        if (lastindex != -1) {
            path = path.substring(0, lastindex);
            System.out.println("Duong dan dau cat 1:" + path);
            lastindex = path.lastIndexOf(File.separator);
            if (lastindex != -1) {
                path = path.substring(0, lastindex);
            }

        }     
    	return path;
    }
}
